package com.yxsd.web.spring;

import com.jolbox.bonecp.BoneCPDataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * AddBeanDefinition
 *
 * @author: zhangteng
 * @time: 2015/6/16 18:28
 */
public class AddBeanDefinition implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClassName("dataSource");
        beanDefinition.setBeanClass(BoneCPDataSource.class);
        beanDefinition.getPropertyValues().add("username", "root");
        beanDefinition.getPropertyValues().add("password", "mysql");
        beanDefinition.getPropertyValues().add("jdbcUrl", "jdbc:mysql://127.0.0.1:3306/db_yxsd?defaultFetchSize=25&amp;useLocalSessionState=true&amp;elideSetAutoCommit=true&amp;useUsageAdvisor=false&amp;useReadAheadInput=false&amp;useUnbufferedInput=false&amp;cacheServerConfiguration=true&amp;autoReconnect=true&amp;zeroDateTimeBehavior=convertToNull");
        beanDefinition.getPropertyValues().add("driverClass", "com.mysql.jdbc.Driver");
        beanDefinition.getPropertyValues().add("idleConnectionTestPeriod", "60");
        beanDefinition.getPropertyValues().add("idleMaxAge", "240");
        beanDefinition.getPropertyValues().add("maxConnectionsPerPartition", "60");
        beanDefinition.getPropertyValues().add("minConnectionsPerPartition", "20");
        beanDefinition.getPropertyValues().add("partitionCount", "3");
        beanDefinition.getPropertyValues().add("acquireIncrement", "10");
        beanDefinition.getPropertyValues().add("statementCacheSize", "50");
        beanDefinition.getPropertyValues().add("releaseHelperThreads", "3");
        beanDefinition.setScope(GenericBeanDefinition.SCOPE_SINGLETON);

        registry.registerBeanDefinition("dataSource", beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
